<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Selfhelp\Controller;
use Think\Controller;

class IndexController extends BaseController {

    /*
	 * 显示我的列表页
	 */
    public function lists(){
        $resource = D(C('TABLE_SELFHELPRESOURCE'));
        $res = $resource->where(array('user_token'=>$_COOKIE['user_userinfo']))->order('id desc')->select();
        $this->assign('list',$res);
        $this->display();
    }

	/*
	 * 手机验证通过保存
	 */
	public function savetel(){
		$tel = I('post.usertel');
		$useropenid = I('post.useropenid');
		$telres = D(C('TABLE_SELFHELP'));
		$num = $telres->where(array('user_tel'=>$tel))->count();
		if($num < 1){
			$data['user_tel'] = $tel;
			$data['user_token'] = $useropenid;
			$data['user_date'] = date("Y-m-d H:i:m");
			$res = $telres->add($data);
			if($res){
				echo 1;//保存成功
			}else{
				echo 2;//保存失败
			}
		}else{
			echo 3;//存在此号码
		}
	}

	/*
	 * 删除当前码
	 */
	public function deletema(){
		$id = I('get.id');
		$ischecknum = preg_match('/^\d*$/',$id);
		if($ischecknum){
			$resource = D(C('TABLE_SELFHELPRESOURCE'));
			$res = $resource->where(array('id'=>$id))->delete();
			if($res){
				header('Location:/Selfhelp/Index/lists');
			}else{
				$this->error('删除失败','',1);
			}
		}
	}

	/*
	 * 删除视频
	 */
	public function deleteres(){
		$p_res = '.'.I('post.p_res');
		$resourceid = I('post.resourceid');
		if(!empty($p_res)){
			if(@unlink($p_res)){
				$this->deletefiletosavadata($resourceid);
				echo 1;
			}else{
				echo 2;
			}
		}else{
			echo 3;
		}
	}

	/*
	 * 删除图片
	 */
	public function deleteimg(){
		$p_res = '.'.I('post.p_res');
		$resourceid = I('post.resourceid');
		$p_res = str_replace('http://h5.hrtv.cn','',$p_res);
		if(!empty($p_res)){
			if(@unlink($p_res)){
				$this->deletefiletosavadata1($resourceid);
				echo 1;
			}else{
				echo 2;
			}
		}else{
			echo 3;
		}
	}

	/*
	 * 删除视频同时更新数据库信息
	 */
	protected function deletefiletosavadata($id){
		$resource = D(C('TABLE_SELFHELPRESOURCE'));
		$data['videourl'] = '';
		$res = $resource->where(array('id'=>$id))->save($data);
	}

	/*
	 * 删除图片同时更新数据库信息
	 */
	protected function deletefiletosavadata1($id){
		$resource = D(C('TABLE_SELFHELPRESOURCE'));
		$data['imgurl'] = '';
		$res = $resource->where(array('id'=>$id))->save($data);
	}

	public function review(){
		$usertoken = $_COOKIE['user_userinfo'];
		$fwei = D(C('TABLE_SELFFWEI'));
		$res = $fwei->where(array('user_token'=>$usertoken))->find();
		$this->assign('result',$res);
		$this->display();
	}

	/*
	 * 判断是否有防伪
	 */
	protected function isfwei($usertoken){
		$fwei = D(C('TABLE_SELFFWEI'));
		$res = $fwei->where(array('user_token'=>$usertoken))->count();
		if($res<1){
			$this->assign('isfwei',0); //无防伪
			//return 0;
		}else{
			$this->assign('isfwei',1); //有防伪
			//return 1;
		}
	}

	/*
	 * 添加防伪
	 */
	public function reviewedit(){
		$rules = array(
				array('epname','require','企业名称不能为空！'),
				array('epbuild','require','生产厂商不能为空！'),
				array('epaddres','require','企业地址不能为空！'),
				array('eptel','require','服务热线不能为空！'),
				array('ep_code','require','邮编不能为空！'),
		);
		$fwei = D(C('TABLE_SELFFWEI'));
		if(!$fwei->validate($rules)->create()){
			$this->error($fwei->getError(),'',1);
		}else{
			$fdate = date("Y-m-d H:i:m");
			$usertoken = $_COOKIE['user_userinfo'];
			$res = $fwei->where(array('user_token'=>$usertoken))->count();
			if($res>0){
				$data['epname'] = I('post.epname');
				$data['epbuild'] = I('post.epbuild');
				$data['epaddres'] = I('post.epaddres');
				$data['eptel'] = I('post.eptel');
				$data['ep_code'] = I('post.ep_code');
				$data['fdate'] = $fdate;
				$result = $fwei->where(array('user_token'=>$usertoken))->save($data);
				$this->alertmesg($result);
			}else{
				$fwei->user_token = $usertoken;
				$fwei->fdate = $fdate;
				$addres = $fwei->add();
				$this->alertmesg($addres);
			}
		}

	}

	protected function alertmesg($res){
		if($res){
			header('Location:/Selfhelp/Index/lists');
		}else if($res==0){
			header('Location:/Selfhelp/Index/lists');
		}else{
			$this->error('更新失败','',1);
		}
	}

	public function changefilename(){
		$fileinfo = I('post.fileinfo');
		echo $fileinfo;
	}

	public function guanzhu(){
//		$issubscribe = $_COOKIE['newissubscribe'];
//		if($issubscribe!=1){
//			header('Location:http://pay.hrtv.cn/Hrmservercenter/Newissubscribe?redirect_url=http://h5.hrtv.cn/Selfhelp/Reg/index');
//		}
		$this->display();
	}

	/*
	 * 预览页面
	 */
	public function preview(){
		$usertoken = $_COOKIE['user_userinfo'];
		$id = I('get.id');
		/*先判断参数是否为数字*/
		$ischecknum = preg_match('/^\d*$/',$id);
		/*根据结果处理以下逻辑*/
		if($ischecknum){
			$resource = D(C('TABLE_SELFHELPRESOURCE'));
			$res = $resource->where(array('id'=>$id))->find();
			if($res){
				if($res['user_token']!=$usertoken){
					$this->assign('shelfedit',1);
				}
				$this->assign('recinfo',$this->resourcetofalgdata(2));       /*获取资源表中推荐的数据*/
				$this->getwenhuacontentplate(); 	/*加载文化内容*/
				$this->getbindplatecontent($id);	/*加载当前码绑定了哪些文化内容*/
				$this->assign('nowaddres',getURL());
				$this->assign('rand',uniqid());
				if($res['status']==2 || !empty(I('get.mtst'))){
					$this->logicinfo($res);
				}else{
					if($res['user_token']==$usertoken){
						$this->logicinfo($res);
					}else if($res['user_token']!=$usertoken && $res['status']!=2){
						$this->assign('status',$res['status']);
						$this->display('noaudit');
					}
				}
			}else{
				echo "空数据！";
			}
		}else{
			echo "非法参数！";
		}
	}

	//根据某条件执行的逻辑
	protected function logicinfo($res){
		$this->getasidscontent();
		$this->assign('info',$res);
		$this->isfwei($res['user_token']);
		$this->display('index');
	}

	/*获取文化内容板块*/
	protected function getwenhuacontentplate(){
		$plateres = D(C('TABLE_PLATERESOURCE'));
		$res = $plateres->where(array('type'=>5))->select();
		$this->assign('platedata',$res);
	}

	/*获取当前码都绑定了哪些文化内容*/
	protected function getbindplatecontent($id){
		$plateres = D(C('TABLE_SELFHELPRESOURCEPLATE'));
		$res = $plateres->where(array('selfhelpresourceid'=>$id))->select();
		$this->assign('datacont',count($res));
		$this->assign('bindwhcontentdata',$res);
	}

	protected function getasidscontent(){
		$asidsstr = "http://h5.hrtv.cn/Home/Index/asids";
		$str = file_get_contents($asidsstr);
		$str = trim($str, "\xEF\xBB\xBF");
		$this->assign('asids',$str);
	}

	/*
	 * 生成二维码
	 */
	public function codeimg(){
		if(empty($_COOKIE['user_userinfo'])){
			header("Location:http://e.hrtv.cn/ehrtv/Login/index?url=".getURL());
		}
		$id = I('get.id');
		/*先判断参数是否为数字*/
		$ischecknum = preg_match('/^\d*$/',$id);
		if($ischecknum){
			$codeurl = C('WEBDOMAIN')."/Selfhelp/id/".$id.".html";
			$this->assign('codeimgurl',$this->qrcode('selfhelp_'.$id,$codeurl));
			$resource = D(C('TABLE_SELFHELPRESOURCE'));
			$res = $resource->where(array('id'=>$id))->find();
			$this->assign('sinfo',$res);
			$this->display();
		}else{
			$this->error('非法参数','',1);
		}
	}

	/*
	 * 更换图片
	 */
	public function changeimgup(){
		$size = 6291456;
		$exts = array('jpg', 'jpeg', 'gif', 'png');
		$rootpath = './IndexPublic/selfhelp/changeimg/';
		$upload = new \Think\Upload();			//实例化上传类
		$upload->maxSize   =     $size;			//设置附件上传大小3M
		$upload->exts      =     $exts;	//设置附件上传类型
		$upload->rootPath  =     $rootpath; 	//设置附件上传根目录
		// 上传单个文件
		$info   =   $_FILES[I('get.myimgfiles')];
		if($info['type']!='image/gif' && $info['type']!='image/jpg' && $info['type']!='image/png' && $info['type']!='image/jpeg'){
			//echo $info['type'];
			echo 1;//不符合上传文件类型
		}else{
			$info1   =   $upload->uploadOne($info);
			if(is_array($info1)){
				if($info1['size']>$size){
					echo 2;//超出限定范围6M
				}else if(empty($info1['name'])){
					echo 3;//没有上传文件
				}else{
					//echo C('WEBDOMAIN').'/'.$info['savepath'].$info['savename']; //返回路径
					echo ltrim($rootpath.$info1['savepath'].$info1['savename'],'.');
				}
			}else{
				echo 0;//上传失败
			}
		}

	}

	/*
	 * 留言
	 */
	public function talk(){
		$user_tel = null;
		$usertoken = $_COOKIE['user_userinfo'];
		$help = D(C('TABLE_SELFHELP'));
		$res = $help->where(array('user_token'=>$usertoken))->find();
		if($res) {
			$user_tel = $res['user_tel'];
		}
		$talk = D(C('TABLE_SELFHELPTALK'));
		$talk->user_token = $usertoken;
		$talk->user_tel = $user_tel;
		$talk->content = html_entity_decode(I('post.content'));
		$talk->cdate = date("Y-m-d H:i:m");;
		$res = $talk->add();
		if($res){
			echo 1;//成功
		}else{
			echo 0;//失败
		}
	}

	public function shouce(){
		$this->display();
	}

	/*
	 *获取资源表中的某扩展属性的数据
	 */
	protected function resourcetofalgdata($attr,$type=null){
		$type = I('get.type');
		$ys = D(C('TABLE_YANGSHENG'));
		$where['y_state'] = 0;
		if(!empty($type)){
			$where['type'] = $type;
		}
		$where['_string'] = "find_in_set('".$attr."',isc)";
		$where['_logic'] = 'and';
		$result = $ys->where($where)->order('y_datetime desc')->select();
		if($result and $result!=0){
			return $result;
		}else{
			return null;
		}
	}



}